<template>
  <div class="box-warp">
    <div class="box">
      <div class="logo"></div>
      <el-form ref="ruleForm" :model="form" label-width="120px" :rules="rules" :label-position="labelPosition">
        <el-form-item label="用户名" prop="name" class="user-name">
          <el-input v-model="form.name" placeholder="请输入用户名">
            <template #prefix>
              <el-icon class="el-input__icon">
                <User />
              </el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item label="密码" prop="pwd" class="user-name">
          <el-input v-model="form.pwd" :prefix-icon="Lock" placeholder="请输入密码" type="password"></el-input>
        </el-form-item>
        <div class="btn">
          <button @click="onSubmit()" type="button">登录</button>
        </div>
      </el-form>
    </div>
  </div>
</template>
<script lang="ts" setup>
import { reactive, ref } from 'vue'
import { loginApi } from '../../api/login/index.js'
import { useStore } from '../../pinia/index'
import { useRouter, useRoute } from 'vue-router'
import { Search, Lock, User } from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'
import { getMenuApi } from '@/api/menu'
import { getRoleByUserIdApi } from '@/api/mgmtUser'
// do not use same name with ref
const router = useRouter()
const labelPosition = ref('top')
const form = reactive({ name: '', pwd: '' })
const rules = reactive({
  name: [
    {
      required: true,
      message: '请输入用户名',
      trigger: 'blur'
    }
  ],
  pwd: [
    {
      required: true,
      message: '请输入密码',
      trigger: 'blur'
    }
  ]
})
const ruleForm = ref(null)
const pinia = useStore()
const onSubmit = () => {
  ruleForm.value.validate(async (valid) => {
    if (valid) {
      let res = await loginApi(form)
      if (res.code === 200) {
        pinia.updataUser(res.data.userInfo, res.data.token)
        let data = await getMenuApi()
        pinia.fomatRoute(data.data)
        // 用户角色
        let role = await getRoleByUserIdApi({ userId: res.data.userInfo.id })
        let arr = role.data.map((item) => {
          return item.name
        })
        pinia.userRoles = arr
        router.push({ path: '/Home' })
      } else {
        ElMessage.error('密码或账号错误')
      }
    }
  })
}
</script>
<style lang="scss" scoped>
.box-warp {
  position: relative;
  width: 100%;
  height: 100vh;
  background: url('../../assets/img/65.jpg');
  background-size: cover;
  // background: linear-gradient(135deg, #75f0db, #75dbf0, #8a75f0, #f075c7);
  // animation: gradientmove 5s ease infinite alternate;
  // background-size: 200% 200%;
}
@keyframes gradientmove {
  0% {
    background-position: 0% 0%;
  }
  100% {
    background-position: 100% 100%;
  }
}
.box {
  width: 350px;
  border-radius: 12px;
  box-shadow: 10px 10px 24px #6d6b6b;
  position: absolute;
  left: 50%;
  top: 50%;
  background: inherit;
  transform: translate(-50%, -50%);
  padding: 20px;
  z-index: 100;
}
.box::before {
  content: '';
  width: calc(100% + 20px);
  height: calc(100% + 20px);
  border-radius: 12px;
  box-shadow: 10px 10px 24px #6d6b6b;
  position: absolute;
  top: -10px;
  left: -10px;
  background: inherit;
  overflow: hidden;
  filter: blur(5px);
  z-index: -1;
}
.btn {
  text-align: center;
  button {
    outline: none;
    padding: 8px 30px;
    cursor: pointer;
    width: 100%;
    background: #388ce9;
    border: none;
    color: #fff;
    border-radius: 5px;
  }
}
.user-name ::v-deep .el-form-item__label {
  font-size: 16px;
  font-weight: 600;
  color: #b3b8bf;
}
</style>
<style>
.box-warp .el-input__inner {
  height: 35px;
  line-height: 35px;
}
</style>
